package com.polaris.live.dialog_fragment

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import com.gyf.immersionbar.ImmersionBar
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.util.NavigationBarUtils
import com.polaris.live.databinding.PopupMusicBinding
import com.polaris.live.ui.party.music.AddMusicFragment
import com.polaris.live.ui.party.music.MusicMainFragment
import com.polaris.live.ui.party.music.listener.AddMusicListener
import com.polaris.live.ui.party.music.listener.MusicMainListener
import com.polaris.live.utils.fragment.attachFragment

/**
 * MusicPopup
 *
 * @author Created by 天晴 on 2024/9/14 19:18
 * @since 1.0.0
 **/
class MusicPopup : DialogFragment(), MusicMainListener, AddMusicListener {

    private lateinit var mBinding: PopupMusicBinding

    private var isAddFragment = false
    private var mUpSeatBack: (() -> Unit)? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        mBinding = PopupMusicBinding.inflate(inflater, container, false)
        initView()
        return mBinding.root
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.apply {
            setWindowAnimations(R.style.BottomDialogAnimation)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        ImmersionBar.with(this)
            .statusBarDarkFont(true)
            .navigationBarDarkIcon(true)
            .transparentNavigationBar()
            .init()

        val bottomView = mBinding.bottomView
        val lp = bottomView.layoutParams as ViewGroup.LayoutParams
        lp.height = NavigationBarUtils.getNavigationBarHeight(requireContext())
        bottomView.layoutParams = lp
    }

    private val musicMianFragment by lazy {
        MusicMainFragment().apply {
            setMusicMainListener(this@MusicPopup)
        }
    }

    private val addMusicFragment by lazy {
        AddMusicFragment().apply {
            setAddMusicListener(this@MusicPopup)
        }
    }

    private fun initView() {
        mBinding.root.setOnSingleClickListener {
            if (isAddFragment) {
                toBack()
            } else {
                dismiss()
            }
        }
        mBinding.flFragment.setOnSingleClickListener {
        }
        childFragmentManager.attachFragment(R.id.fl_fragment) {
            musicMianFragment
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = Dialog(requireContext(), android.R.style.Theme_Light_NoTitleBar_Fullscreen)
        val window = dialog.window
        window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog.setOnKeyListener { _, keyCode, event ->
            if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_UP) {
                if (isAddFragment) {
                    toBack()
                    true
                } else {
                    false
                }
            } else {
                false
            }
        }
        return dialog
    }

    companion object {

        fun show(context: AppCompatActivity, upSeat: () -> Unit) {
            val fragmentManager = context.supportFragmentManager
            MusicPopup().apply {
                setToUpSeatListener(upSeat)
            }.show(fragmentManager, "music")
        }

        fun show(context: Fragment, upSeat: () -> Unit) {
            val fragmentManager = context.parentFragmentManager
            MusicPopup().apply {
                setToUpSeatListener(upSeat)
            }.show(fragmentManager, "music")
        }
    }

    override fun toAdd() {
        isAddFragment = true
        childFragmentManager.attachFragment(R.id.fl_fragment) {
            addMusicFragment
        }

    }

    override fun close() {
        dismiss()
    }

    fun setToUpSeatListener(upSeat: () -> Unit) {
        mUpSeatBack = upSeat
    }

    /**
     * 去上座
     */
    override fun toUpSeat() {
        mUpSeatBack?.invoke()
    }

    override fun toBack() {
        isAddFragment = false
        childFragmentManager.attachFragment(R.id.fl_fragment) {
            musicMianFragment.apply {
                setData()
            }
        }
    }

}